package org.filter4osgi.builder;


import static org.filter4osgi.builder.FilterBuilder.*

public class FilterTest extends GroovyTestCase {


    public void testEquality() {
        def result = eq('mailboxName', 'welcome')
        assert '(mailboxName=welcome)' == result.toString()
    }

    public void testEquality2() {
        def result = eq('foo', 'bar')
        assert '(foo=bar)' == result.toString()
    }

    public void testApprox() {
        def result = approx('mailboxName', 'welcome')
        assert '(mailboxName~=welcome)' == result.toString()
    }

    public void testStartsWith() {
        def result = startsWith('foo', 'bar')
        assert '(foo=bar*)' == result.toString()
    }

    public void testEndsWith() {
        def result = endsWith('foo', 'bar')
        assert '(foo=*bar)' == result.toString()
    }

    public void testContains() {
        def result = contains('foo', 'bar')
        assert '(foo=*bar*)' == result.toString()
    }

    public void testGreaterThan() {
        def result = gte('count', 0)
        assert '(count>=0)' == result.toString()
    }

    public void testLessThan() {
        def result = lte('count', 11)
        assert '(count<=11)' == result.toString()
    }

    public void testAnd() {
        def result = and(
                eq('mailboxName', 'welcome'),
                eq('lang', 'en')
        )
        assert '(&(mailboxName=welcome) (lang=en))' == result.toString()
    }

    public void testOr() {
        def result = or(
                eq('mailboxName', 'welcome'),
                eq('lang', 'en')
        )
        assert '(|(mailboxName=welcome) (lang=en))' == result.toString()
    }

    public void testNot() {
        def result = not(eq('mailboxName', 'welcome'))
        assert '(!(mailboxName=welcome))' == result.toString()
    }

    public void testNot_MultipleClauses() {
        def result = not(
                or(
                        eq('mailboxName', 'welcome'),
                        eq('mailboxName', 'smtp')
                )
        )
        assert '(!(|(mailboxName=welcome) (mailboxName=smtp)))' == result.toString()
    }

    public void testAndOrNot_with3ArityAnd() {
        def result = and(
                eq('mailboxName', 'welcome'),
                or(
                        eq('lang', 'de'),
                        eq('lang', 'en*')
                ),
                not(
                        eq('lang', 'en_CA')
                )
        )
        assert '(&(mailboxName=welcome) (|(lang=de) (lang=en*)) (!(lang=en_CA)))' == result.toString()
    }

    public void testAndOrNot_with3ArityOr() {
        def result = or(
                eq('mailboxName', 'welcome'),
                and(
                        eq('lang', 'de'),
                        eq('mailboxID', '5')
                ),
                and(
                        eq('lang', 'en_CA'),
                        eq('mailboxID', '9')
                )
        )
        assert '(|(mailboxName=welcome) (&(lang=de) (mailboxID=5)) (&(lang=en_CA) (mailboxID=9)))' == result.toString()
    }

    public void testIsPresent() {
        def result = exists('mailboxName')
        assert '(mailboxName=*)' == result.toString()
    }

    public void testIsType() {
        def result = isType(StringBuilder.class)
        assert '(objectClass=java.lang.StringBuilder)' == result.toString()
    }
}